package com.hcb.tonguetiphealth.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.hcb.tonguetiphealth.model.Wine;

public class FoodTypeManager {
	private DBManager db;
	private String tableName;
	private List<Wine> list;
	private int recordCount = 0;
	private List<String> foodTypeList = new ArrayList<String>();
	private Map<String, List<String>> foodTypeMap = new HashMap<String, List<String>>(); 
	

	public FoodTypeManager(Activity ctx) {
		db = DBManager.getInstance(ctx);
		tableName = DBManager.table_foodtype;
	}
	
	/**
	 * 保存美食类别
	 * @param Map<String, List<String>> 
	 * @return
	 */
	public void saveFoodType(Map<String, List<String>> foodTypeMap) {
		db.deleteAll(tableName);
		ContentValues values;
		 
		 Set<Map.Entry<String, List<String>>> set = foodTypeMap.entrySet();
	        for (Iterator<Map.Entry<String, List<String>>> it = set.iterator(); it.hasNext();) {
	            Map.Entry<String, List<String>> entry = (Map.Entry<String, List<String>>) it.next();
	            String  foodtype = entry.getKey();
	            List<String> list = entry.getValue();//美食子类
	            for(int i=0; i<list.size(); i++){
	            	values = new ContentValues();
	    			values.put("foodtype", foodtype);
	    			values.put("childfoodtype", list.get(i));
	    			db.insert(tableName, values);
	            }
	        }
	 
	}
	
	/**
	 * 获取美食类别
	 * @return
	 */
	public Map<String, List<String>> getFoodTypeMap() {
		Map<String, List<String>> foodTypeMap = new HashMap<String, List<String>>();
		 
		String strSql  = "SELECT DISTINCT foodtype  from " + tableName;
		Cursor mCursor = db.getCursorBySql(strSql);
		if (null != mCursor) {
			while(mCursor.moveToNext()) {
			 foodTypeList.add(mCursor.getString(0));
			 System.out.println(mCursor.getString(0));
			}
			mCursor.close();
		}
		
		for (int i = 0; i < foodTypeList.size(); i++) {
			String foodType = foodTypeList.get(i);
			System.out.println("-----"+foodType);
			 strSql  = "select childfoodtype from " + tableName +" where foodtype = '" + foodType + "'";
			// System.out.println(strSql);
			 mCursor = db.getCursorBySql(strSql);
			 List<String> list = new ArrayList<String>();
			 if(null != mCursor) {
				while(mCursor.moveToNext()) {
				 list.add(mCursor.getString(0));
				// System.out.println(mCursor.getString(0));
				}
				mCursor.close();
			}else {
				System.out.println("NUll------------------------");
			}
			foodTypeMap.put(foodType, list);
		}
		return foodTypeMap;
	}
	
	public int getRecordCount() {
		return recordCount;
	}
	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}

	public List<String> getFoodTypeList() {
		return foodTypeList;
	}
	 
	 
	public void setFoodTypeMap(Map<String, List<String>> foodTypeMap) {
		this.foodTypeMap = foodTypeMap;
	}
	 
}
